package com.zwb.aiwebsite.controller;

import com.zwb.aiwebsite.entity.product.TbProductType;
import com.zwb.aiwebsite.entity.solution.TbSolutionType;
import com.zwb.aiwebsite.service.TbProductService;
import com.zwb.aiwebsite.service.TbSolutionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Comparator;
import java.util.List;

/**
 * @author 2825075112@qq.com
 */
@Controller
@AllArgsConstructor
public class IndexController {

    private final TbProductService tbProductService;
    private final TbSolutionService tbSolutionService;

    @GetMapping("/")
    public String index( Model model){
        addAttribute(model);
        return "/index";
    }

    @GetMapping("/{page}")
    public String index(@PathVariable String page, Model model){
        addAttribute(model);
        return page;
    }

    @GetMapping("/{page1}/{page2}")
    public String abouts( @PathVariable String page1, @PathVariable String page2, Model model){
        addAttribute(model);
        return page1+ "/" + page2;
    }

    @GetMapping("/admin/{page}")
    public String adminPage(@PathVariable String page, Model model){
        return "/admin/"+page;
    }

    @GetMapping("/admin")
    public String adminPage(Model model){
        return "/admin/index";
    }

    @GetMapping("/product/menu/{id}")
    public String productMenuPage(Model model, @PathVariable Integer id){
        addAttribute(model);
        tbProductService.productMenu(id, model);
        return "/product/universal";
    }



    @GetMapping("/product/info/{id}")
    public String productInfoPage(Model model, @PathVariable Integer id){
        addAttribute(model);
        tbProductService.productInfoPage(model, id);
        return "/product/info";
    }

    @GetMapping("/solution/menu/{id}")
    public String solutionMenuPage(Model model, @PathVariable Integer id){
        addAttribute(model);
        tbSolutionService.SolutionMenu(id, model);
        return "/solution/apply";
    }

    @GetMapping("/solution/info/{id}")
    public String solutionInfoPage(Model model, @PathVariable Integer id){
        addAttribute(model);
        tbSolutionService.SolutionInfoPage(model, id);
        return "/solution/info";
    }

    public void addAttribute(Model model){
        //产品中心菜单
        List<TbProductType> allProductType = tbProductService.getAllProductType();
        allProductType.sort(Comparator.comparing(TbProductType::getOrder));
        model.addAttribute("productTypeList", allProductType);
        //解决方案菜单
        List<TbSolutionType> allSolutionType = tbSolutionService.getAllSolutionType();
        allSolutionType.sort(Comparator.comparing(TbSolutionType::getOrder));
        model.addAttribute("solutionTypeList", allSolutionType);
    }


    @GetMapping("/favicon.ico")
    public String favicon(){
        //todo 先让不报错
        return "index";
    }

}
